<?php header("Content-type: text/javascript"); ?>

/* 
 * Functions related to blocks.
 */

// Make a block

var idInt = 0;

function blocksInit(){
    $.ajaxSetup ({
        cache: false
    });
}

function makeBlock(id){
    $.get(
        'ajax/retrieveBlock/'+id,
        function(responseText){
            $('#mainContent').append(responseText);

            $('#block-'+id).data("toggled", "false")

            $('#block-'+id).draggable({
                handle: 'h2',
                containment: 'parent',
                snap: true,
                stop: function(){ savePosition(id,$(this)); }
            });
                
            $('#block-'+id).resizable({
                stop    :   function(){ savePosition(id,$(this)); },
            });

            $('#block-'+id).bind('dblclick',function(){
                $(this).find('h2').blindToggle('40','slow','easeInQuad',function(){
                    savePosition(id,$(this))
                });
            });
            
            function savePosition(id,e){
            console.log($(e).data("toggled"));
                $.post(
                    'ajax/setBlockPosition',
                    {
                        top     :   $(e).css('top'),
                        left    :   $(e).css('left'),
                        height  :   $(e).css('height'),
                        width   :   $(e).css('width'),
                        blockId :   id
                    }
                );
            };

            if($('#block-'+id).attr('top')!="" & $('#block-'+id).attr('left')!=""){
                storedTop = parseInt($('#block-'+id).attr('top')) + 73;
                storedLeft = parseInt($('#block-'+id).attr('left')) + 10;

                $('#block-'+id).offset({
                    top: storedTop,
                    left: storedLeft
                });
            }

            $('#block-'+id).css('height', $('#block-'+id).attr('height')+"px");
            $('#block-'+id).css('width', $('#block-'+id).attr('width')+"px");

            $("#edit-"+id).button({
                icons: {
                    primary: 'ui-icon-pencil'
                },
                text: false,
            }).bind('click',function(){
                $("#dialog").load('dialog/editBlock/'+id+'/',
                    function(){
                        $('#dialog').dialog();
                    }
                );
            }).css('margin-top','-3px').css({
                'vertical-align'    :   'middle',
                'float'             :   'right',
                'right'             :   '20px',
                'width'             :   '20px'
            })

            $("#delete-"+id).button({
                icons: {
                    primary: 'ui-icon-trash'
                },
                text: false,
            }).bind('click',function(){
                $.post('ajax/deleteBlock/'+id,
                    function(e){
                        $("#block-"+id).remove();
                });
               
            }).css('margin-top','-3px').css({
                'vertical-align'    :   'middle',
                'float'             :   'right',
                'right'             :   '20px',
                'width'             :   '20px'
            })
        }
    );
}
